package zhongbo.java.air.airflightpath.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.airflightpath.mapper.AirFightPathMapper;
import zhongbo.java.air.airflightpath.model.AirFlightPath;
import zhongbo.java.air.airflightpath.service.IAirFightPathService;
import zhongbo.java.air.common.vo.PageVo;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class AirFightPathServiceImpl implements IAirFightPathService<AirFlightPath> {

    @Autowired
    private AirFightPathMapper airFightPathMapper;

    @Override
    public AirFlightPath queryById(String id) {
        AirFlightPath airFlightPath = airFightPathMapper.selectById(id);
        if (airFlightPath == null){
            throw new RuntimeException("查询失败！当前id：" + id);
        }
        return airFlightPath;
    }

    @Override
    public PageVo<AirFlightPath> queryByPage(Integer pageNumber, Integer pageSize) {
        QueryWrapper<AirFlightPath> queryWrapper  = new QueryWrapper<>();
        //飞机ICAO码排序
        queryWrapper.orderByAsc("icao_code");
        IPage<AirFlightPath> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirFlightPath> airFlightPathIPage = airFightPathMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(airFlightPathIPage);
    }

    @Override
    public PageVo<AirFlightPath> queryByPageTime(Integer pageNumber, Integer pageSize
            , String icao, String startTime, String endTime
            , String keyword, String fields
            , String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirFlightPath> queryWrapper  = new QueryWrapper<>();
        if (icao.isEmpty()|| icao.equals("")){
            throw new RuntimeException("ICAO地址码不能为空");
        }
        queryWrapper.eq("icao_code",icao);

        if (startTime != null && endTime != null){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
            LocalDateTime startTimed = LocalDateTime.parse(startTime, formatter);
            LocalDateTime endTimed = LocalDateTime.parse(endTime, formatter);
            queryWrapper.between("timestamp",startTimed,endTimed);
        }
        //支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        //支持指定字段排序
        if (StrUtil.isNotBlank(oderByField) && orderByAsc){
            queryWrapper.orderByAsc(oderByField);
        }
        IPage<AirFlightPath> pageParam = new Page<>(pageNumber,pageSize);
        IPage<AirFlightPath> airFlightPathIPage = airFightPathMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(airFlightPathIPage);
    }

    @Override
    public AirFlightPath insert(AirFlightPath airFightPath) {
//        if (airFightPathMapper.exists(new QueryWrapper<AirFlightPath>().eq("icao_code", airFightPath.getIcaoCode()))) {
//            throw new RuntimeException("ICAO地址码已存在");
//        }
        int insert = airFightPathMapper.insert(airFightPath);
        if (insert <= 0){
            throw new RuntimeException("添加失败 ->>> " + airFightPath);
        }
        return airFightPath;
    }

    @Override
    public AirFlightPath update(AirFlightPath airFlightPath) {
        if (airFightPathMapper.exists(new QueryWrapper<AirFlightPath>().eq("icao_code", airFlightPath.getIcaoCode()).ne("id",airFlightPath.getId()))) {
            throw new RuntimeException("ICAO地址码已存在");
        }
        int i = airFightPathMapper.updateById(airFlightPath);
        if (i <= 0){
            throw new RuntimeException("修改失败 ->>> id：" + airFlightPath.getId() + " 不存在； 实体信息 ->>> " + airFlightPath);
        }
        return airFlightPath;
    }

    @Override
    public void deleteById(String id) {
        int i = airFightPathMapper.deleteById(id);
        if (i <= 0){
            throw new RuntimeException("删除失败 ->>> id：" + id);
        }
    }
}
